package com.fashion.web.actions;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fashion.model.News;
import com.fashion.model.gallery.Album;
import com.fashion.model.person.AdditionalText;
import com.fashion.model.person.Person;
import com.fashion.service.NewsService;
import com.fashion.service.gallery.AlbumService;
import com.fashion.service.person.PersonService;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller
@Namespace("/person")
public class PersonAction extends ActionSupport {
    private static final Logger logger = Logger.getLogger(BrandAction.class);

    private String firstChar = "a";

    private List<Person> tops;
    @Resource(name = "personService")
    private PersonService personService;
    @Resource(name = "newsService")
    private NewsService newsService;
    @Resource(name = "albumService")
    private AlbumService albumService;
    private Person person;
    private List<Person> personList = new ArrayList<Person>();
    private List<News> newsList;
    private List<Album> albumList;

    @Action(value = "index", results = {@Result(name = SUCCESS, location = "person.jsp")})
    public String index() throws Exception {
        tops = personService.find(0, 12, "priority");
        if ("others".equals(firstChar)) {
            personList = personService.findNotStartWithEn(0, Integer.MAX_VALUE);
        } else {
            personList = personService.findByFirstChar(firstChar, 0,
                    Integer.MAX_VALUE);
        }
        return SUCCESS;
    }

    @Action(value = "find_person", results = {@Result(name = SUCCESS, location = "person_detail.jsp")})
    public String find() throws Exception {
        try {
            person = personService.findById(person.getId());
            if (null == person) {
                return SUCCESS;
            }

            List<String> keywords = new ArrayList<String>();
            String name = person.getName().replace(" ", "$");
            if (name != null && !"".equals(name.trim())) {
                keywords.add(name);
            }
            newsList = newsService.searchTagNews(keywords, 1, 4);
            albumList = albumService.findByKeywords(keywords, 0, 3);
            personList = personService.find(0, 6, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    @Action(value = "find_person_story", results = {@Result(name = SUCCESS, location = "person_detail_story.jsp")})
    public String findStory() throws Exception {
        person = personService.findById(person.getId());
        personList = personService.find(0, 6, null);
        return SUCCESS;
    }

    @Action(value = "find_person_news", results = {@Result(name = SUCCESS, location = "person_detail_news.jsp")})
    public String findNews() throws Exception {
        person = personService.findById(person.getId());
        List<String> keywords = new ArrayList<String>();
        String name = person.getName().replace(" ", "$");
        if (name != null && !"".equals(name.trim())) {
            keywords.add(name);
        }
        newsList = newsService.searchTagNews(keywords, 1, 10);
        personList = personService.find(0, 6, null);
        return SUCCESS;
    }

    @Action(value = "find_person_gallery", results = {@Result(name = SUCCESS, location = "person_detail_gallery.jsp")})
    public String findGallery() throws Exception {
        person = personService.findById(person.getId());
        List<String> keywords = new ArrayList<String>();
        String name = person.getName().replace(" ", "$");
        if (name != null && !"".equals(name.trim())) {
            keywords.add(name);
        }
        albumList = albumService.findByKeywords(keywords, 0, 20);
        personList = personService.find(0, 6, null);
        return SUCCESS;
    }

    public List<Person> getPersonList() {
        return personList;
    }

    public String getFirstChar() {
        return firstChar;
    }

    public void setFirstChar(String firstChar) {
        this.firstChar = firstChar;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public List<News> getNewsList() {
        return newsList;
    }

    public List<Person> getTops() {
        return tops;
    }

    public List<Album> getAlbumList() {
        return albumList;
    }
}
